package com.project.lk.nurseapp.bloodglucose;

import android.os.Bundle;

import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.interfaces.datasets.IBarDataSet;
import com.github.mikephil.charting.utils.ColorTemplate;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.project.lk.nurseapp.R;
import com.project.lk.nurseapp.base.BaseActivity;
import com.project.lk.nurseapp.utils.AppManager;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
/**
 * 功能介绍：
 * 项目名：
 * 模块名称：
 * 作者：LK
 * 日期：2018/1/9
 * 修改日期：
 */
public class BloodGlucoseHistoryActivity extends BaseActivity {


    @BindView(R.id.xrecyclerview)
    XRecyclerView xrecyclerview;
    @BindView(R.id.history_chart)
    BarChart historyChart;

    @Override
    protected void initData() {

        initChart();
    }

    private void initChart() {
        historyChart.setDrawBarShadow(false);
        historyChart.setDrawValueAboveBar(true);
        historyChart.getDescription().setEnabled(false);
        // 如果60多个条目显示在图表,drawn没有值
//        historyChart.setMaxVisibleValueCount(60);
        // 扩展现在只能分别在x轴和y轴
        historyChart.setPinchZoom(false);
        //是否显示表格颜色
        historyChart.setDrawGridBackground(false);
        XAxis xAxis = historyChart.getXAxis();
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setDrawGridLines(false);
        // 只有1天的时间间隔
        xAxis.setGranularity(1f);
        xAxis.setLabelCount(7);
//        xAxis.setAxisMaximum(50f);
//        xAxis.setAxisMinimum(0f);

        YAxis leftAxis = historyChart.getAxisLeft();
        leftAxis.setLabelCount(8, false);
        leftAxis.setPosition(YAxis.YAxisLabelPosition.OUTSIDE_CHART);
        leftAxis.setSpaceTop(15f);
        //这个替换setStartAtZero(true)
        leftAxis.setAxisMinimum(0f);
        leftAxis.setAxisMaximum(50f);

        YAxis rightAxis = historyChart.getAxisRight();
        rightAxis.setEnabled(false);
        // 设置标示，就是那个一组y的value的
        Legend l = historyChart.getLegend();
        l.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM);
        l.setHorizontalAlignment(Legend.LegendHorizontalAlignment.LEFT);
        l.setOrientation(Legend.LegendOrientation.HORIZONTAL);
        l.setDrawInside(false);
        //样式
        l.setForm(Legend.LegendForm.SQUARE);
        //字体
        l.setFormSize(9f);
        //大小
        l.setTextSize(11f);
        ArrayList<BarEntry> yVals1 = new ArrayList<BarEntry>();
        l.setXEntrySpace(4f);
        //模拟数据
        yVals1.add(new BarEntry(1, 10));
        yVals1.add(new BarEntry(2, 20));
        yVals1.add(new BarEntry(3, 30));
        yVals1.add(new BarEntry(4, 40));
        yVals1.add(new BarEntry(5, 50));
        yVals1.add(new BarEntry(6, 10));
        yVals1.add(new BarEntry(7, 20));
        yVals1.add(new BarEntry(8, 30));
        yVals1.add(new BarEntry(9, 40));

        setData(yVals1);
    }
    //设置数据
    private void setData(ArrayList yVals1) {
        float start = 1f;
        BarDataSet set1;
        if (historyChart.getData() != null &&
                historyChart.getData().getDataSetCount() > 0) {
            set1 = (BarDataSet) historyChart.getData().getDataSetByIndex(0);
            set1.setValues(yVals1);
            historyChart.getData().notifyDataChanged();
            historyChart.notifyDataSetChanged();
        } else {
            set1 = new BarDataSet(yVals1, "血糖记录表");
            //设置有2种颜色
            set1.setColors(new int[]{ColorTemplate.rgb("#11B6A3"),ColorTemplate.rgb("#FF00A0EB")});
            ArrayList<IBarDataSet> dataSets = new ArrayList<IBarDataSet>();
            dataSets.add(set1);
            BarData data = new BarData(dataSets);
            data.setValueTextSize(10f);
            data.setBarWidth(0.9f);
            //设置数据
            historyChart.setData(data);
        }
    }

    @Override
    protected void initView() {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_blood_glucose_history;
    }


    @OnClick(R.id.back)
    public void onViewClicked() {
        AppManager.getAppManager().finishActivity();
    }
}
